﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aluetjen.EmailService.Domain.Infrastructure;
using Aluetjen.EmailService.Domain.Models.Pipeline;

namespace Aluetjen.EmailService.Web.Controllers
{
    public class GatewayController : Controller
    {
        private readonly IRepository<Gateway> _gatewayRepository;
        private readonly IUnitOfWork _unitOfWork;

        public GatewayController(IRepository<Gateway> gatewayRepository, IUnitOfWork unitOfWork)
        {
            _gatewayRepository = gatewayRepository;
            _unitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View(_gatewayRepository.All());
        }

        //
        // GET: /Gateway/Details/5

        public ActionResult Details(int id)
        {
            return View(_gatewayRepository.FindBy(g => g.Id == id));
        }

        //
        // GET: /Gateway/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Gateway/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Gateway/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Gateway/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Enable(int id)
        {
            return ModifyGateway(id, gateway => gateway.DispatchToGateway = true);
        }

        [HttpPost]
        public ActionResult Disable(int id)
        {
            return ModifyGateway(id, gateway => gateway.DispatchToGateway = false);
        }

        private ActionResult ModifyGateway(int id, Action<Gateway> action)
        {
            var gateway = _gatewayRepository.FindBy(g => g.Id == id);

            if (gateway == null)
            {
                throw new Exception("Gateway not found!");
            }

            action(gateway);

            _unitOfWork.Commit();

            return RedirectToAction("Index");
        }

        //
        // GET: /Gateway/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Gateway/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
